package m.jclecai.com.dialog;

import android.content.Context;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.utils.ToastUtil;

/**
 * Created by Dinosa on 2017/12/11.
 * 这里是晒单的对话框；
 */

public class ShowOrderDialog extends BaseDialog {
    private final String mMoney;
    private final String mOneMuiltyMoney;
    @BindView(R.id.tv_plan_money)
    TextView mTvPlanMoney;
    @BindView(R.id.tv_one_multiple_money)
    TextView mTvOneMultipleMoney;

    @BindView(R.id.rb_open)
    RadioButton mRbOpen;

    @BindView(R.id.rb_open_stop)
    RadioButton mRbOpenStop;

    @BindView(R.id.et_profit)
    EditText mEtProfit;

    @BindView(R.id.iv_helper)
    ImageView mIvHelper;

    @BindView(R.id.et_content_slogan)
    EditText mEtContentSlogan;

    @BindView(R.id.tv_cancel)
    TextView mTvCancel;

    @BindView(R.id.tv_sure)
    TextView mTvSure;

    @BindView(R.id.tv_rule_desc)
    TextView mTvRulesDesc;

    @BindView(R.id.rg_mode)
    RadioGroup mRgMode;

    @BindView(R.id.ll_profit)
    LinearLayout mLlProfit;

    @BindView(R.id.ll_xuanyan)
    LinearLayout ll_xuanyan;

    public ShowOrderDialog(Context context, OnClickButtonListener listener, String money, String oneMuiltyMoney) {
        super(context, true);
        mMoney = money;
        mOneMuiltyMoney = oneMuiltyMoney;
        initView();
        initData();
        initListener();
        mListener = listener;
    }

    private void initListener() {

        mRgMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_open:
                        //公开
                        mLlProfit.setVisibility(View.GONE);
                        mTvRulesDesc.setVisibility(View.GONE);
                        break;
                    case R.id.rb_open_stop:
                        //发起大神单
                        mLlProfit.setVisibility(View.VISIBLE);
                        ll_xuanyan.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_open_secrecy:
                        //截止后公开
                        mLlProfit.setVisibility(View.GONE);
                        ll_xuanyan.setVisibility(View.GONE);
                        break;
                }
            }
        });
        mEtProfit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //这里我们要做一个判断；
                String profitStr = s.toString();
                if (!TextUtils.isEmpty(s)) {
                    //这个是盈利率
                    int profit = Integer.parseInt(profitStr);
                    if (profit > 10) {
                        mEtProfit.setText("10");
                        mEtProfit.setSelection("10".length());
                        ToastUtil.showToast(mContext, "盈利率不能大于10%");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void initData() {

    }

    private void initView() {
        setContentView(R.layout.dialog_show_order);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        String cont = "<html>" + "方案金额:  " + "<font color='red'>" + mMoney + "</font></html>";

        mTvPlanMoney.setText(Html.fromHtml(cont));

        String danBei = "<html>" + "单倍金额:  " + "<font color='red'>" + mOneMuiltyMoney + "</font></html>";

        mTvOneMultipleMoney.setText(Html.fromHtml(danBei));
    }

    /**
     * 这里是返回Html样式的文字的；
     *
     * @return
     */
    public String createHtmlStr(String content) {


        return "";
    }

    //方案的模式
    public static final String MODE = "mode";
    //提成
    public static final String PROFIT = "profit";
    //宣言
    public static final String SLOGAN = "slogan";

    @OnClick(R.id.tv_sure)
    public void onClickSure(View view) {
        //将参数传递回去；这里以bundle的形式传递回去；
//        ["公开", "截止后公开","对跟单用户公开（保密）", "截止后对跟单用户公开"]
//        分别对应
//        0,1,2,3
        String yiliticheng = mEtProfit.getText().toString();
        String xuanyan = mEtContentSlogan.getText().toString();
        int checkedRadioButtonId = mRgMode.getCheckedRadioButtonId();
        String mode = "0";
        if (checkedRadioButtonId == R.id.rb_open) {
            //这里表示公开：
            mode = "0";
        } else if (checkedRadioButtonId == R.id.rb_open_stop) {
            //这里表示的是截止后公开；
            mode = "1";
        } else if (checkedRadioButtonId == R.id.rb_open_secrecy) {
            //这里表示保密：
            mode = "2";
            xuanyan = "";
            yiliticheng = "";
        } else {
            mode = "3";
        }

        if (mListener != null) {
            mListener.onClickSure(mode, yiliticheng, xuanyan);
        }
        dismiss();

    }

    @OnClick(R.id.tv_cancel)
    public void onClickCancel(View view) {
        //点击的取消的按钮；
        if (mListener != null) {
            mListener.onClickCancel();
        }
        dismiss();
    }

    @OnClick(R.id.iv_helper)
    void onClickHelp() {
        if (mTvRulesDesc.getVisibility() == View.GONE) {
            mTvRulesDesc.setVisibility(View.VISIBLE);
        } else {
            mTvRulesDesc.setVisibility(View.GONE);
        }
    }

    public OnClickButtonListener mListener;

    /**
     * 点击按钮的监听事件
     */
    public interface OnClickButtonListener {

        /**
         * 如果点击的是确定的生成方案的话，就会带三个参数过去
         *
         * @param mode   模式、公开、截止后公开
         * @param profit 提成
         * @param slogan 方案宣言
         */
        public void onClickSure(String mode, String profit, String slogan);

        /**
         * 当用户点击了取消的按钮；
         */
        public void onClickCancel();

    }

    public void show(String money, String oneMuiltyMoney) {

        String cont = "<html>" + "方案金额: " + "<font color='red'>" + money + "</font></html>";
        mTvPlanMoney.setText(Html.fromHtml(cont));
        String danbei = "<html>" + "单倍金额: " + "<font color='red'>" + oneMuiltyMoney + "</font></html>";
        mTvOneMultipleMoney.setText(Html.fromHtml(danbei));
        show();
    }

}
